package com.glodon.paas.document.web.resource.internal;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Path;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.glodon.paas.consts.StringConst;
import com.glodon.paas.document.web.resource.FileResource;
import com.glodon.paas.document.web.util.SecurityUtils;

@Component
@Path("/internal/file")
public class FileInternalResource extends FileResource {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileInternalResource.class);

    @Override
    public String getUserId(HttpServletRequest request) {
        String userId;
        String filePath = (String) request.getAttribute("filePath");
        if (filePath != null && filePath.startsWith("userId" + StringConst.SEPARATOR_SLASH)) {
            userId = filePath.split(StringConst.SEPARATOR_SLASH)[1];
            request.setAttribute("filePath",
                                 filePath.replace("userId" + StringConst.SEPARATOR_SLASH + userId
                                                  + StringConst.SEPARATOR_SLASH, ""));
        } else {
            userId = SecurityUtils.getRequestUserId(request);
        }
        return userId;
    }

    @Override
    public Logger getLoger() {
        return LOGGER;
    }
}
